<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRoleFn"
          >新增角色</el-button>
        </el-row>
        <!-- 新增弹框 -->
        <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="closeDialogFn">
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="closeDialogFn">取消</el-button>
              <el-button size="small" type="primary" @click="comfireFn">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="small" type="success" @click="assignPerssionFn(scope.row.id)">分配权限</el-button>
              <el-button size="small" type="primary" @click="editRoleFn(scope.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRoleFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :total="total"
            :page-size="params.pagesize"
            @current-change="changePageFn"
          />
        </el-row>
      </el-card>
    </div>
    <AssignPerssion :show-assign-dialog="showAssignDialog" :cur-id="curId" @close-dialog="closeAssignDialogFn" />
  </div>
</template>
<script>
import { getRoleListAPI, deleteRoleAPI, addRoleAPI, getRoleDetailAPI, updateRoleAPI } from '@/api/setting'
import AssignPerssion from './components/AssignPerssion.vue'
export default {
  name: 'Setting',
  components: {
    AssignPerssion
  },
  data() {
    return {
      list: [], // 角色数组对象
      params: {
        page: 1,
        pagesize: 3
      },
      total: 0,
      showDialog: false, // 弹框显示与隐藏
      roleForm: {
        name: '',
        description: ''
      }, //
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      showAssignDialog: false, // 分配权限弹框
      curId: null// 当前角色id
    }
  },
  mounted() {
    this.getRoleList()
  },
  methods: {
    // 获取数据
    async  getRoleList() {
      const res = await getRoleListAPI(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    changePageFn(newPage) {
      this.params.page = newPage
      this.getRoleList()
      console.log(newPage)
    },
    // 删除角色
    async  delRoleFn(id) {
      this.$confirm('确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除接口
        await deleteRoleAPI(id)
        // 当最后一页数据只有一个，我们让他回到前一页
        this.list.length <= 1 && this.params.page > 1 && this.params.page--
        // 重新获取角色列表
        this.getRoleList()
        // 提示用户信息
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '删除失败'
        })
      })
    },
    closeDialogFn() { // ❌关闭弹框
      this.roleForm = {
        name: '',
        description: ''
      }// 清空数据
      this.$refs.roleForm.resetFields()// 清空校验痕迹
      this.showDialog = false
    },
    // 新增角色
    addRoleFn() {
      this.showDialog = true
    },
    comfireFn() { // 兜底校验
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          // 如果检验正确，
          // 编辑
          if (this.roleForm.id) {
            await updateRoleAPI(this.roleForm)
            this.$message.success('更新角色成功')
          } else {
            // 新增
            await addRoleAPI(this.roleForm)
            this.$message.success('新增角色成功')
          }
          this.getRoleList()// 重新获取数据
          this.closeDialogFn()
        }
      })
    },
    async editRoleFn(id) { // 编辑角色
      this.getRoleDetail(id)// 回填
    },
    async  getRoleDetail(id) {
      this.showDialog = true
      const res = await getRoleDetailAPI(id)
      this.roleForm = res.data
    },
    // 分配权限
    assignPerssionFn(id) {
      this.curId = id
      this.showAssignDialog = true
    },
    // 关闭分配权限弹框
    closeAssignDialogFn() {
      this.showAssignDialog = false
    }
  }
}
</script>
